package com.qf.courtesycard.mqcustomer.rabbitmq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.courtesycard.mqcustomer.entity.Userforticket;
import com.qf.courtesycard.mqcustomer.service.IUserforticketService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 刘欣
 * @createTime 2023/3/11 10:06
 * @description
 */
@Component
@Slf4j
public class TicketLogDBCustomer {

    @Autowired
    private IUserforticketService userforticketService;

    @RabbitListener(queues = "queue_ticketDB_logs")
    public void getTicketLog(String msg, Channel channel, Message message) throws Exception {
        log.debug("msg={},channel={},message={}", msg, channel, message);
        ObjectMapper objectMapper = new ObjectMapper();
        Userforticket userforticket = objectMapper.readValue(msg, Userforticket.class);
        userforticketService.saveTicketLogs(userforticket);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
